package com.qf.controller;

import com.qf.service.GoodsService;
import com.qianfeng.util.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class GoodsController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    GoodsService goodsService;

    @GetMapping("/goods")
    public ResultMsg findAll(int page ,int size){

        return goodsService.findAll(page,size);
    }

    @GetMapping("/testgoods")
    public  ResultMsg test1(String key){
        String val="";
        Boolean aBoolean = redisTemplate.hasKey(key);
        if(!aBoolean){//t1 t2 ....t100
            log.info("查询的是："+key+"值，此时进入数据库。。。。。");
            //将当前不存在的key值存储到redis中，并设置一个过期时间
            redisTemplate.opsForValue().set(key,key);
            redisTemplate.expire(key,10, TimeUnit.SECONDS);
        }else{
            log.info("redis..........");
             val=redisTemplate.opsForValue().get(key);
        }
        return ResultMsg.success(val);
    }
}
